package com.wcsy.myblog.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.wcsy.myblog.dao.ArticleMapper;
import com.wcsy.myblog.dao.CommentMapper;
import com.wcsy.myblog.dao.UserMapper;
import com.wcsy.myblog.entity.*;
import com.wcsy.myblog.service.*;
import com.wcsy.myblog.service.serviceImpl.ArticleServiceImpl;
import com.wcsy.myblog.service.serviceImpl.CollectServiceImpl;
import com.wcsy.myblog.service.serviceImpl.CosServiceImpl;
import com.wcsy.myblog.service.serviceImpl.NumServiceImpl;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpSession;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * 文章
 */
@RestController
@RequestMapping("/article")
@CrossOrigin
@Api(description = "文章")
@ApiModel
public class ArticleController {
    @Autowired
    private IArticleService iArticleService;
    @Autowired
    private HttpSession session;
    @Autowired
    CosServiceImpl cosService;
    @Autowired
    IUserService iUserService;
    @Autowired
    IImgService iImgService;
    @Autowired
    NumServiceImpl numService;
    @Autowired
    ICollectService iCollectService;
    @Autowired
    CommentMapper commentMapper;
    @Autowired
    CollectServiceImpl collectService;
    @Autowired
    ILkplusService iLkplusService;
    @Autowired
    IAttentionService iAttentionService;
    @Autowired
    UserMapper userMapper;
    @Autowired
    ArticleMapper articleMapper;

    /**
     * @apiNote 查询登陆用户的总文章信息
     * */
    @ApiOperation("查询登陆用户的总文章信息")
    @GetMapping("/selectLogin")
    public JsonResult selectLogin(){
        User user = (User) session.getAttribute("user");
        //查询出该用户的文章
        try {
            List<Article> articles = iArticleService.select(user.getUserName());
            return new JsonResult(200,"查询成功",articles);
        } catch (Exception e) {
            e.printStackTrace();
            return new JsonResult(500,"查询失败","null");
        }
    }
    /**
     * @apiNote 查询总文章，显示在主页
     * */
    @ApiOperation("查询所有文章显示在主页")
    @GetMapping("/selectList")
    public JsonResult selectList(){
        try {
            List<Article> articles = articleMapper.selectList();
            return new JsonResult(200,"查询成功",articles);
        } catch (Exception e) {
            e.printStackTrace();
            return new JsonResult(500,"查询失败",null);
        }
    }
    /**
     * 显示点赞数前六名的文章及访问量前6的文章
     * @return map.get("articlesLike")点赞数前六名的文章
     *          map.get("articlesPageview")访问量前6的文章
     */
    @ApiOperation("显示点赞数前六名的文章及访问量前6的文章")
    @GetMapping("/sixLike")
    public JsonResult sixLike(){
        try {
            Map<String, Object> map = iArticleService.selectList();
            return new JsonResult(200,"查询成功",map);
        } catch (Exception e) {
            e.printStackTrace();
            return new JsonResult(500,"查询失败",null);
        }
    }
    /**
     *查询作者的总文章信息
     * */
    @ApiOperation("查询作者的总文章信息")
    @GetMapping("/select")
    public JsonResult select(String userName){//@RequestBody
        try {
            List<Article> articles = iArticleService.select(userName);
            return new JsonResult(200,"查询成功",articles);
        } catch (Exception e) {
            e.printStackTrace();
            return new JsonResult(500,"查询失败","null");
        }
    }

    /**
     * private Article article;
     * private Label label;
     * private String typeName;
     * private String typeKidName;
     * @return
     */
    @ApiOperation("添加文章")
    @PostMapping("/insert")
    public JsonResult insert(@RequestBody InserntArticle inserntArticle){
        if (inserntArticle.getTypeName() ==null){
            inserntArticle.setTypeName("-1");
        }else if (inserntArticle.getTypeKidName() ==null){
            inserntArticle.setTypeKidName("-1");
        }
        try {
            iArticleService.insertArticle(inserntArticle.getArticle(),inserntArticle.getLabel(),
                                inserntArticle.getTypeName(),inserntArticle.getTypeKidName());
            return new JsonResult(200,"添加成功","");
        } catch (Exception e) {
            e.printStackTrace();
//            e.getMessage().toString();
            return new JsonResult(500,"添加失败","");
        }
    }

    @ApiOperation("上传文章内容图片")
    @PostMapping("insertPage")
    public JsonResult insertPage(@RequestParam(value = "pic", required = false) MultipartFile file){
        try {
            String page = numService.getfile(file);
            System.out.println(page);
            iImgService.insertImg(page);
            return new JsonResult(200,"添加图片成功",page);
        } catch (Exception e) {
            e.printStackTrace();
            return new JsonResult(500,"添加图片失败","");
        }
    }
    @ApiOperation("删除文章内容图片")
    @GetMapping("deletePage")
    public JsonResult deletePage(String aImg){
        boolean a_img = iImgService.remove(new QueryWrapper<Img>().eq("a_img", aImg));
        if (a_img){
            return new JsonResult(200,"删除成功","");
        }
        return new JsonResult(500,"删除失败","");
    }
    /**
     * @apiNote 用户传过来文章的id，将该文章的具体内容显示出来
     * @param articleId 用户传过来的文章id
     * @return
     */
    @ApiOperation("查询某一篇文章的信息")
    @GetMapping("/selectArticle")
    public JsonResult selectArticle(int articleId){
        User user = (User) session.getAttribute("user");
//                User user = userMapper.selectOne(new QueryWrapper<User>().eq("user_id", 1));
        try {
            Map<Object, Object> map = new LinkedHashMap<>();
            //访问量+1
            iArticleService.updatePageviews(articleId);
            //显示整篇文章的信息
            Article article = iArticleService.serlectOneArticle(articleId);
            //文章被收藏的数量
            List<Collect> i = collectService.selectCount(articleId);
            if (!i.isEmpty()){
                article.setCollectNumber(i.size());
            }else {
                article.setCollectNumber(0);
            }
            //判断 用户是否已收藏这篇文章 收藏返回ture
            article.setIscollect(iCollectService.SelectIsCollect((int) user.getUserId(),articleId));
            //判断 用户是否已点赞这篇文章 已赞返回ture
           article.setLick(iLkplusService.selectIsLike((int) user.getUserId(),articleId));
           //判断用户是否 已关注这个文章的作者 已关注返回ture
            article.setAttention(iAttentionService.selectUserAttention((int)article.getUserId(), (int) user.getUserId()));
            //显示该文章的评论
            List<Comment> comments = commentMapper.selectOneComment(articleId);
            //显示和该文章相似标题的文章
            List<Article> cosList = cosService.cosList(articleId);
            map.put("article",article);
            map.put("comments",comments);
            map.put("cosList",cosList);
            return new JsonResult(200,"查询文章成功",map);
        } catch (Exception e) {
            e.printStackTrace();
            return new JsonResult(500,"查询失败","");
        }
    }
    /**
     * @apiNote 查询所有文章 进行分页
     * @param page 页面数
     */
    @ApiOperation("查询所有文章 进行分页")
    @GetMapping("/listPage")
    public JsonResult listPage(int page){
        try {
            ArticlePage<Article> articlePage = iArticleService.PageSelect(page);
            return new JsonResult(200,"分页成功",articlePage);
        } catch (Exception e) {
            e.printStackTrace();
            return new JsonResult(500,"分页失败","");
        }
    }

    /**
     * @apiNote  查询某个用户的所有文章  进行分页
     * @param userId  用户id
     * @param page   页面数
     * @return
     */
    @ApiOperation("查询某个用户的所有文章进行分页")
    @GetMapping("/listUserPage")
    public JsonResult listUserPage(int page,int userId){
        try {
            ArticlePage<Article> articleArticlePage = iArticleService.PageUserSelect(page, userId);
            return new JsonResult(200,"分页成功",articleArticlePage);
        } catch (Exception e) {
            e.printStackTrace();
            return new JsonResult(500,"分页失败","");
        }
    }
    /**
     * @apiNote 查询用户收藏的文章
     * @param userId
     * @return
     */
    @ApiOperation("查询用户收藏的文章")
    @GetMapping("/selectCollect")
    public JsonResult selectCollect(int userId){
        try {
            List<Article> articles = iArticleService.SelectListCollect(userId);
            return new JsonResult(200,"查询成功",articles);
        } catch (Exception e) {
            e.printStackTrace();
            return new JsonResult(500,"查询失败","");
        }
    }
    /**
     * @apiNote 添加收藏
     * @param articleId 被收藏的文章id
     */
    @ApiOperation("添加收藏")
    @GetMapping("insertCollect")
    public JsonResult insertCollect(int articleId){
        try {
            iArticleService.insertCollect(articleId);
            return new JsonResult(200,"收藏成功","");
        } catch (Exception e) {
            e.printStackTrace();
            return new JsonResult(500,"收藏失败","");
        }
    }

    /**
     * @apiNote 取消收藏
     * @param articleId 待取消收藏的文章id
     * @return
     */
    @ApiOperation("取消收藏")
    @GetMapping("deleteCollect")
    public JsonResult deleteCollect(int articleId){
       User  user = (User) session.getAttribute("user");
        boolean remove = iCollectService.remove(new QueryWrapper<Collect>()
                .eq("user_id", user.getUserId())
                .eq("article_id", articleId));
        if (remove){
            return new JsonResult(200,"删除成功","");
        }
        return new JsonResult(500,"删除失败","");
    }
    /**
     * @apiNote 点赞
     * @param articleId  文章id
     * @return
     */
    @ApiOperation("点赞")
    @GetMapping("/like")
    public JsonResult likeArticle(int articleId,boolean toLike){
        try {
            iArticleService.updateNublike(articleId,toLike);
            return new JsonResult(200,"成功","");
        } catch (Exception e) {
            e.printStackTrace();
            return new JsonResult(500,"失败","");
        }
    }

    /**
     * 修改文章  除文章内容图片外
     * @param article  用户传过来要修改文章的内容  一定要有文章id
     * @return
     */
    @ApiOperation("修改文章")
    @GetMapping("/updateArticle")
    public JsonResult updateArticle(Article article) throws Exception {
        String throwss = "";
        if (article.getArticleName()!=null){
            String getfilter = numService.getfilter(article.getArticleName());
            if(!getfilter.equals(article.getArticleName())){
                throwss="请勿输入敏感词汇!!!";
            }
        }
        //确定文章内容中是不是含有敏感词汇
        if(article.getArticleContent()!=null){
            String getfilter1 = numService.getfilter(article.getArticleContent());
            article.setArticleContent(getfilter1);
        }
        boolean b = iArticleService.updateArticle(article);
        if (b){
            return new JsonResult(200,"修改文章成功","");
        }
        return new JsonResult(500,"修改文章失败",throwss);
    }
    /**
     * 删除文章
     */
    @ApiOperation("删除文章")
    @GetMapping("removeArticle")
    public JsonResult removeArticle(int articleId){
        try {
            iArticleService.removeArticle(articleId);
            return new JsonResult(200,"success",null);
        } catch (Exception e) {
            e.printStackTrace();
            return new JsonResult(500,"fail",null);
        }
    }

    /**
     * 新增的未查看的点赞 评论 粉丝
     * @return map.get("nolike")点赞数
     *         map.get("userComment")评论数
     *         map.get("userFan")粉丝数
     */
    @ApiOperation("新增的未查看的点赞数 评论数 粉丝数")
    @GetMapping("insertNo")
    public JsonResult insertNo(){
        try {
            Map<String, Integer> map = numService.getNumber();
            return new JsonResult(200,"成功",map);
        } catch (Exception e) {
            e.printStackTrace();
            return new JsonResult(500,"失败","");
        }
    }

    /**
     * 清空未查看的
     * @param name like:"点赞",
     *             comment:"评论"
     *             Fan:"粉丝";
     */
    @ApiOperation("清空未查看的")
    @GetMapping("deleteNoView")
    public JsonResult deleteNoView(String name){
        try {
            numService.deleteNo(name);
            return new JsonResult(200,"归零成功","");
        } catch (Exception e) {
            e.printStackTrace();
            return new JsonResult(500,"归零失败","");
        }
    }
}
